import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:my_audio_recorder_example/Bean/AudioRecordItem.dart';
import 'package:my_audio_recorder_example/Ext/ColorUtil.dart';
import 'package:my_audio_recorder_example/Page/RecordPage.dart';
import 'package:my_audio_recorder_example/Widget/RefreshListWidget.dart';

import '../Test/DisplayPage.dart';
import '../Util/ObjectBox.dart';

class AudioPage extends StatefulWidget {
  const AudioPage({super.key});

  @override
  State<StatefulWidget> createState() => _AudioPage();
}

class _AudioPage extends State<AudioPage> {
  List<AudioRecordItem> list = [];

  @override
  void initState() {
    loadAudioData();
    super.initState();
  }

  final listController = Get.put(ListController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: ColorUtil.stringColor("#e5e6e4"),
        appBar: AppBar(
          title: const Text('录音测试'),
        ),
        floatingActionButton: GestureDetector(
            onTap: () {
              // 前往录音
              Navigator.push(
                      context,
                      CupertinoPageRoute(
                          builder: (context) => const RecordPage()))
                  .then((val) => val ? loadAudioData() : null);
            },
            child: Image.asset('images/arecord.png', width: 60, height: 60)),
        body: list.length == 0
            ? Center(
                child: Text('还没有录音文件\n点击右下角前往录音~'),
              )
            : RefreshListWidget(recordItemList: list));
  }

  /// 加载缓存的录音数据
  void loadAudioData() {
    List<AudioRecordItem> allData = getAllDesc();
    print('-------------loadAudioData-------------\n$allData');
    setState(() {
      list = allData;
      print('-------------loadAudioData XXXXXX-------------\n$list');
    });
  }
}

class ListController extends GetxController {
  List<AudioRecordItem> list = getAllDesc();

  void updateList(List<AudioRecordItem> list) {
    print('--------------ListController updateList---------------');
    this.list = list;
  }
}
